package com.atguigu.day01;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

//从socket读取数据，计算word count
public class Example1 {
    //注意抛出异常
    public static void main(String[] args) throws Exception{
        //获取执行的上下文环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //设置并行任务的数量为1
        env.setParallelism(1);

        //读取数据源
        //先启动：nc -lk 9999
        DataStreamSource<String> source = env.socketTextStream("hadoop102", 9999);

        //map操作
        // 使用flatMap
        // (hello, 1)
        // (world, 1)
        // flatMap的语义：将列表或者流中的每一个元素，转换成0个，1个或者多个元素
        //flatMap是无状态算子
        SingleOutputStreamOperator<Tuple2<String, Integer>> mappedStream = source
                //匿名类
                //第一个泛型是：输入的泛型
                //第二个泛型是：输出的泛型
                .flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public void flatMap(String in, Collector<Tuple2<String, Integer>> out) throws Exception {
                        //使用空格切分字符串
                        String[] array = in.split(" ");
                        for (String word : array) {
                            out.collect(
                                    Tuple2.of(word, 1)
                            );

                        }
                    }
                });

        //shuffle
        //将不同单词的元组shuffle到不同的逻辑分区
        KeyedStream<Tuple2<String, Integer>, String> keyedStream = mappedStream
                .keyBy(new KeySelector<Tuple2<String, Integer>, String>() {
                    @Override
                    public String getKey(Tuple2<String, Integer> value) throws Exception {
                        return value.f0;
                    }
                });


        //reduce
        //将相同逻辑分区的数据进行聚合
        //sum是有状态算子
        SingleOutputStreamOperator<Tuple2<String, Integer>> reducedStream = keyedStream
                .sum("f1");

        //打印
        reducedStream.print();




        //提交并执行程序
        env.execute();

    }
}
